<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<input id="inp" onkeyup="print_tone()"> <span id="res"></span>
<script type="text/javascript">
CONS="กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ"
VOW_BEFORE="เแโใไ"
VOW_UPDOWN="ัิีึืุู็ำ"
VOW_AFTER="ะาอยว"
MARKS="่้๊๋"
HIGH=2
MID=1
LOW=0
CONS_HIGH="ขฃฐถผฝศษสห"
CONS_MID="กจฎฏดตบปอ"
CONS_DEAD="กดบ"
SHORT_VOW=["ะ","ั","ิ","ึ","ุ","เะ","เ็","แะ","แ็","โะ","","เาะ","็อ","เอะ","เียะ","เือะ","ัวะ","ิว","เ็ว","เา","ัย","ใ","ไ","็อย","ุย","ำ"]
LONG_VOW=["า","ี","ื","ือ","ู","เ","แ","โ","อ","็","เอ","เิ","เีย","เือ","ัว","เว","แว","าว","เียว","าย","อย","โย","เย","วย","เือย"]
LT="Low tone"
FT="Falling tone"
MT="Middle tone"
RT="Rising tone"
HT="High tone"

function in_array(v,a)
{
	for(i=0;i<a.length;i++)if(a[i]==v)return true;
	return false;
}

function get_class(cons)
{
	if(CONS.indexOf(cons)==-1)return false;
	if(CONS_HIGH.indexOf(cons)!=-1)return HIGH;
	if(CONS_MID.indexOf(cons)!=-1)return MID;
	return LOW;
}

function get_tone()
{
	s=document.getElementById('inp').value;
	cons1="";
	cons2="";
	vow="";
	mark="";
	for(i=0;i<s.length;i++)
	{
		if(cons2!="")return "Error 1";
		if(VOW_BEFORE.indexOf(s[i])!=-1)
		{
			if(i>0)return "Error 2";
			vow=s[i];
			continue;
		}
		if(VOW_UPDOWN.indexOf(s[i])!=-1)
		{
			if(cons1=="")return "Error 7";
			vow+=s[i];
			continue;
		}
		if(VOW_AFTER.indexOf(s[i])!=-1)
		{
			if(cons1==""&&CONS.indexOf(s[i])==-1)return "Error 3";
			if(cons1!="")
			{
				vow+=s[i];
				continue;
			}
		}
		if(CONS.indexOf(s[i])!=-1)
		{
			if(cons1=="")cons1=s[i];else cons2=s[i];
			continue;
		}
		if(MARKS.indexOf(s[i])!=-1)
		{
			if(cons1=="")return "Error 4";
			if(mark!="")return "Error 5";
			mark=s[i];
			continue;
		}
		return "Error 6";
	}
	if(cons1=="")return "Error 9";
	cclass=get_class(cons1);
	if(mark!="")
	{
		if(mark=="่")
		{
			if(cclass==HIGH||cclass==MID)return LT;
			else if(cclass==LOW)return FT;
			return "Error 10";
		}
		else if(mark=="้")
		{
			if(cclass==HIGH||cclass==MID)return FT;
			else if(cclass==LOW)return HT;
			return "Error 11";
		}
		else if(mark=="๊"&&cclass==MID)return HT;
		else if(mark=="๋"&&cclass==MID)return RT;
		return "Error 12";
	}
	if(vow==""&&cons2=="ร")long=true;
	else if(in_array(vow,SHORT_VOW))long=false;
	else if(in_array(vow,LONG_VOW))long=true;
	else return "Error 8";
	if(cons2!="")
	{
		if(CONS_DEAD.indexOf(cons2)!=-1)
		{
			if(cclass==HIGH||cclass==MID)return LT;
			if(cclass==LOW)if(long)return FT;else return HT;
			return "Error 13";
		}
		if(cclass==HIGH)return RT;
		else if(cclass==MID||cclass==LOW)return MT;
		return "Error 14";
	}
	if(long)
	{
		if(cclass==HIGH)return RT;
		if(cclass==MID||cclass==LOW)return MT;
		return "Error 15";
	}
	if(cclass==HIGH||cclass==MID)return LT;
	if(cclass==LOW)return HT;
	return "Error 16";
}

function print_tone()
{
	document.getElementById('res').innerHTML=get_tone();
}
</script>
</body>
